package lanou.giftsay_01.main;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import lanou.giftsay_01.tools.GiftUser;
import lanou.giftsay_01.R;
import lanou.giftsay_01.tools.BaseActivity;

/**
 * Created by dllo on 16/8/29.
 */
public class LogInActivity extends BaseActivity {

    private ImageView iv;
    private Button button;
    private EditText et;
    private EditText etPs;
    private Button button2;
    private GiftUser giftUser;
    private TextView tv;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

        iv = (ImageView) findViewById(R.id.btn_close);
        button = (Button) findViewById(R.id.btn_login);
        button2 = (Button) findViewById(R.id.btn_zhuce);
        et = (EditText) findViewById(R.id.et_phoneNumber);
        etPs = (EditText) findViewById(R.id.et_password);
        tv = (TextView) findViewById(R.id.yanzhengma_tv);
    }

    @Override
    protected void initDate() {
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setVisibility(View.GONE);
                button2.setVisibility(View.VISIBLE);
            }
        });
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
               @Override
            public void onClick(View v) {
             giftUser =new GiftUser();
             giftUser.setUsername(et.getText().toString());
             giftUser.setPassword(etPs.getText().toString());
             giftUser.login(new SaveListener<GiftUser>() {
                 @Override
                 public void done(GiftUser giftUser, BmobException e) {
                     if ( e == null){
                         Toast.makeText(LogInActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                          finish();
                     }else {
                         Toast.makeText(LogInActivity.this, "新用户请注册", Toast.LENGTH_SHORT).show();
                         button.setVisibility(View.GONE);
                         button2.setVisibility(View.VISIBLE);
                     }

                 }
             });



            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                giftUser = new GiftUser();
                giftUser.setUsername(et.getText().toString());
                giftUser.setPassword(etPs.getText().toString());
                giftUser.signUp(new SaveListener<GiftUser>() {
                    @Override
                    public void done(GiftUser giftUser, BmobException e) {
                        if (e == null){
                            Toast.makeText(LogInActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                              button2.setVisibility(View.GONE);
                              button.setVisibility(View.VISIBLE);

                        }else{
                            Toast.makeText(LogInActivity.this, "注册失败或已注册", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

}
